/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.mengfly.product.api;

import cn.mengfly.product.entity.Product;
import cn.mengfly.product.entity.ProductComment;
import cn.mengfly.product.repository.ProductCommentRepository;
import cn.mengfly.product.repository.ProductRepository;
import cn.mengfly.product.service.UserRemoteService;
import cn.mengfly.product.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品管理的Endpoint
 *
 * @author CD826
 * @since 1.0.0
 */
@RestController
@RequestMapping("/products")
public class ProductEndpoint {
    protected Logger logger = LoggerFactory.getLogger(ProductEndpoint.class);
    @Value("${server.port}")
    public int port;

    @Autowired
    private ProductRepository productRepository;
    @Autowired
    private ProductCommentRepository productCommentRepository;
    @Autowired
    private UserService userService;

    @Autowired
    @Qualifier(value = "restTemplate")
    private RestTemplate restTemplate;

    @RequestMapping(method = RequestMethod.GET)
    public List<Product> list() {
        return this.productRepository.findAll();
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Product detail(@PathVariable Long id) {
        return this.productRepository.getOne(id);
    }

    @RequestMapping(value = "/{id}/comments", method = RequestMethod.GET)
    public ProductCommentsDto comments(@PathVariable Long id) {
        List<ProductComment> commentList = this.productCommentRepository.findByProductIdOrderByCreated(id);
        final Product one = this.productRepository.getOne(id);

        ProductCommentsDto dto = new ProductCommentsDto();
        dto.setProduct(one);
        dto.setPort(port);

        dto.addComments(commentList.stream().map((comment) -> {
            ProductCommentsDto.CommentDto commentDto = new ProductCommentsDto.CommentDto(comment);
            commentDto.setAuthor(userService.load(comment.getAuthorId()));
            return commentDto;
        }).collect(Collectors.toList()));

        return dto;
    }

    // 这是使用
//    protected UserDto loadUser(Long userId) {
//        return this.restTemplate.getForEntity("http://USERSERVICE/users/{id}", UserDto.class, userId).getBody();
//    }
}